package com.inossem.warehousemanage_tv;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.inossem.warehousemanage_tv.adapter.DutyAdapter;
import com.inossem.warehousemanage_tv.bean.BaseBean;
import com.inossem.warehousemanage_tv.bean.WelBean;
import com.inossem.warehousemanage_tv.custom.RollViewPager;
import com.inossem.warehousemanage_tv.utils.Api;
import com.inossem.warehousemanage_tv.utils.ToastUtils;
import com.inossem.warehousemanage_tv.utils.requestUtils.InossemCallback;
import com.inossem.warehousemanage_tv.utils.requestUtils.RetrofitUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Response;

/**
 * 欢迎页
 *
 * @author 詹建宇
 * @time on 2019/3/5 15:12
 * @email jianyu.zhan@inossem.com
 */
public class WelComeActivity extends FragmentActivity {

    private final static String json = "{\"notice_list\":[{\"content\":\"原定于3月6日6:59分关闭的无限乱斗模式，现延续开放至4月3日6:59分。同时，已加入金铲铲俱乐部的玩家，可继续在无限乱斗模式中享受俱乐部特权至模式关闭。\"}],\"news_list\":[{\"content\":\"您已进入仓库区域，库区严禁烟火！\"},{\"content\":\"这个是测试消息1\"},{\"content\":\"这个是测试消息2\"}],\"banner_list\":[{\"address\":\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1551850510751&di=bf6716507363f08d01c4cb4580141430&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201807%2F13%2F20180713175041_mjGtz.jpeg\"},{\"address\":\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1551850547535&di=6a8455b8a8f238b910767ddfc49b66d5&imgtype=0&src=http%3A%2F%2Fdingyue.nosdn.127.net%2Fkp%3D3siAuXOyu7HFDwwV08n4b6%3DYJxY1x2PfdJMm8eAlTL1529551274567.jpeg\"},{\"address\":\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1551850547533&di=3a0a43ac5314be8e5c9244d60e68efa8&imgtype=0&src=http%3A%2F%2Fcomic.people.com.cn%2FNMediaFile%2F2014%2F1013%2FMAIN201410131353000344514941064.jpg\"},{\"address\":\"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1551850547525&di=df2f49abe1329827998a3dbcb7d98333&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201506%2F29%2F20150629125034_GuC8h.jpeg\"}],\"duty_list\":[{\"user_name\":\"佳佳\",\"duty_time\":\"2018-10-11 12:12:18\"},{\"user_name\":\"哲总\",\"duty_time\":\"2018-10-12 12:12:18\"}]}";

    private TextView tv_notice;
    private RollViewPager vp_img, vp_msg;
    private ListView lv;

    private long touchTime;

    private List<WelBean.NoticeListBean> noticeList;//公告
    private List<WelBean.BannerListBean> bannerList;//轮播图
    private List<WelBean.DutyListBean> dutyList;//值班人员
    private List<WelBean.NewsListBean> newsList;//消息

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        initView();
        initClick();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    private void initView() {
        tv_notice = findViewById(R.id.tv_notice);
        vp_img = findViewById(R.id.vp_img);
        vp_msg = findViewById(R.id.vp_msg);
        lv = findViewById(R.id.lv);
        tv_notice.setSelected(true);
    }

    private void initData() {
//        RetrofitUtils.getRetrofit(this, "詹建宇", "张志丹", "首页", "首页数据集合")
//                .create(Api.class)
//                .getWelInfo("2", "1", "10", "10", "3")
//                .enqueue(new InossemCallback<BaseBean<WelBean>>() {
//                    @Override
//                    public void success(Response<BaseBean<WelBean>> response) {
//                        WelBean welBean = response.body().getBody();
//
//                        noticeList = welBean.getNotice_list();
//                        bannerList = welBean.getBanner_list();
//                        dutyList = welBean.getDuty_list();
//                        newsList = welBean.getNews_list();
//
//                        setData();
//                    }
//
//                    @Override
//                    public void failure(Response<BaseBean<WelBean>> response) {
//                        ToastUtils.showToast(response.body().getHead().getMsg());
//                    }
//                });

        WelBean welBean = new Gson().fromJson(json, WelBean.class);

        noticeList = welBean.getNotice_list();
        bannerList = welBean.getBanner_list();
        dutyList = welBean.getDuty_list();
        newsList = welBean.getNews_list();

        setData();

    }

    private void initClick() {

    }

    private void setData() {
        //公告
        if (null != noticeList && !noticeList.isEmpty()) {
            tv_notice.setText(noticeList.get(0).getContent());
        }

        //轮播图
        if (null != bannerList && !bannerList.isEmpty()) {
            List<View> viewList = new ArrayList<>();
            for (int i = 0; i < bannerList.size(); i++) {
                ImageView imageView = new ImageView(this);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                viewList.add(imageView);
            }
            vp_img.setAdapter(viewList, true, 4000, 2000);
            for (int i = 0; i < viewList.size(); i++) {
                Glide.with(this).load(bannerList.get(i).getAddress()).into((ImageView) viewList.get(i));
            }
        }

        //值班人员
        if (null != dutyList && !dutyList.isEmpty()) {
            lv.setAdapter(new DutyAdapter(this, dutyList));
        }

        //消息
        if (null != newsList && !newsList.isEmpty()) {
            List<View> viewList = new ArrayList<>();
            for (int i = 0; i < newsList.size(); i++) {
                TextView textView = (TextView) LayoutInflater.from(this).inflate(R.layout.view_wel_news, null);
                textView.setText(newsList.get(i).getContent());
                viewList.add(textView);
            }
            vp_msg.setAdapter(viewList, false, 5000, 2000);
        }
        vp_img.setIsRunning(true);
        vp_msg.setIsRunning(true);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touchTime = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_MOVE:
                touchTime = 0;
                break;
            case MotionEvent.ACTION_UP:
                if (touchTime != 0) {
                    long nowTime = System.currentTimeMillis();
                    if (nowTime - touchTime <= 1000) {
                        jumpToMainActivity();
                    }
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private void jumpToMainActivity() {
        final EditText et = new EditText(this);
        new AlertDialog.Builder(this).setTitle("请输入密码")
                .setView(et)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        startActivity(new Intent(WelComeActivity.this, MainActivity.class));
                    }
                }).setNegativeButton("取消", null).show();
    }

    @Override
    protected void onPause() {
        super.onPause();
        vp_img.setIsRunning(false);
        vp_msg.setIsRunning(false);
    }
}
